import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Like, Repository } from 'typeorm';
import { TagEntity } from './entities/tag.entity';

@Injectable()
export class TagService {
  constructor(
    @InjectRepository(TagEntity)
    private readonly tagRepository: Repository<TagEntity>,
  ) {}

  async create(name) {
    return await this.tagRepository.save({ name });
  }

  findByName() {}

  async findByIds(ids: string[]) {
    return this.tagRepository.findByIds(ids);
  }

  async findAll(query){
    const { pageNum = 1, pageSize = 100, ...params } = query;
    const result = await this.tagRepository.findAndCount({
      where:query.name?{ name:Like(`%${query.name}%`)}:'true',
      order: {
        id: 'DESC',
      },
      skip: (pageNum - 1) * pageSize,
      take: pageSize,
    });
    const list = result[0].map((item) => item);
    return { list, count: result[1] };
  }
}
